package com.example.administrator.myapplication.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;


import com.example.administrator.myapplication.StartGameActivity;
import com.example.administrator.myapplication.base.BaseSurfaceView;
import com.example.administrator.myapplication.R;

import java.text.SimpleDateFormat;
import java.util.TimeZone;


/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class StartGameSurfaceView extends BaseSurfaceView {
    private Bitmap mBigImg,mSmallImg, mTimeImg, mReplayImg;
    //得到屏幕的高度与宽度
    private int mScreenWidth;
    private int mScreenHeight;
    //游戏开始时间
    private long mStartTime;
    //游戏总共耗时
    private long mTime;
    //传入图片ID与游戏等级
    private int mImageId;
    private int mLevel;
    //时间正则
    private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private StartGameActivity mContext;
    //供自己new 的构造方法
    public StartGameSurfaceView(Context context,int imageId,int level) {
        super(context);
        mImageId = imageId;
        mLevel = level;
        mContext= (StartGameActivity)context;
        init();
    }

    public StartGameSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        //获得图片资源
        mBigImg = BitmapFactory.decodeResource(getResources(),mImageId);
        mTimeImg = BitmapFactory.decodeResource(getResources(), R.drawable.k);
        mReplayImg = BitmapFactory.decodeResource(getResources(),R.drawable.up);

        //得到屏幕大小
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        mScreenWidth = metrics.widthPixels;
        mScreenHeight = metrics.heightPixels;
        //缩放图片
        mBigImg = Bitmap.createScaledBitmap(mBigImg,mScreenWidth,mScreenHeight/8*7,true);
        mSmallImg = Bitmap.createScaledBitmap(mBigImg,mScreenWidth/6,mScreenHeight/8,true);
        mTimeImg = Bitmap.createScaledBitmap(mTimeImg,mScreenWidth/12,mScreenHeight/16,true);
        mReplayImg = Bitmap.createScaledBitmap(mReplayImg,mScreenWidth/12,mScreenHeight/16,true);
        //初始时间
        mStartTime = System.currentTimeMillis();
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

    }
    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onPaint(Canvas canvas, Paint paint) {
        //绘制图片
        canvas.drawBitmap(mBigImg,0,mScreenHeight/8,paint);
        canvas.drawBitmap(mSmallImg,0,0,paint);
        canvas.drawBitmap(mTimeImg,mScreenWidth/2,0,paint);
        canvas.drawBitmap(mReplayImg,mScreenWidth/2,mScreenHeight/16,paint);
        paint.setColor(Color.RED);
        paint.setTextSize(40);
        canvas.drawText(getTime(),mScreenWidth/3*2,mScreenHeight/16,paint);
    }
    //时间
    private String getTime(){
        long mTime = System.currentTimeMillis()-mStartTime;
        return sdf.format(mTime);
    }
    //触摸屏幕

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mContext.startActivity(mLevel, mTime /1000);
        return super.onTouchEvent(event);
    }
}
